package com.yiwei.jmx.view;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Configuration;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.yiwei.jmx.R;
import com.yiwei.jmx.activity.BankBindActivity;
import com.yiwei.jmx.adapter.BankSelectDialogAdapter;
import com.yiwei.jmx.api.Api;
import com.yiwei.jmx.api.model.BankCardListModel;
import com.yiwei.jmx.util.Network;
import com.yiwei.jmx.util.RxLifeUtil;
import com.yiwei.jmx.util.ToastUtil;
import com.yiwei.jmx.util.Util;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/3/16 0016.
 */
public class BankSelectDialog extends Dialog {


    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.btn_close)
    ImageView btnClose;
    @BindView(R.id.btn_add)
    LinearLayout btnAdd;
    @BindView(R.id.loading)
    CircularProgress loading;


    private Context context;
    private BankSelectDialogAdapter adapter;
    private boolean initFinish = false;

    public BankSelectDialog(Context context) {
        super(context, R.style.mydialog);
        this.context = context;
        init();
    }

    public BankSelectDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public BankSelectDialog setOnSelect(OnSureListener listener) {
        selectListener = listener;
        return this;
    }

    @Override
    public void show() {
        super.show();
        getBankCard();
    }

    public void getBankCard() {
        if (!Network.checkNetwork(context)) {
            ToastUtil.show(context, "网络连接失败");
            return;
        }

        loading.setVisibility(View.VISIBLE);
        Api.api_service.getBankcardList().compose(RxLifeUtil.checkOn(context)).
                doFinally(() -> {
                    try {
                        loading.setVisibility(View.GONE);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.update(result.data);
                });
    }


    @OnClick({R.id.btn_close, R.id.btn_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_close:
                dismiss();
                break;
            case R.id.btn_add:
                Util.startActivity(context, BankBindActivity.class);
                dismiss();
                break;
        }
    }


    public interface OnSureListener {
        void onSure(BankCardListModel.DataBean item);
    }

    private OnSureListener selectListener;

    private void init() {
        Window window = this.getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);
        window.getDecorView().setPadding(0, 0, 0, 0);
        window.setWindowAnimations(R.style.mydialog_animation);
        setContentView(R.layout.dialog_bank_select);
        ButterKnife.bind(this);

        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {// 横屏
            lp.width = Util.getDisplayMetrics(context).heightPixels;
        } else {
            lp.width = Util.getDisplayMetrics(context).widthPixels;
        }
        window.setAttributes(lp);
        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        this.setCanceledOnTouchOutside(false);

        adapter = new BankSelectDialogAdapter(context);
        adapter.setOnChooseListener(new BankSelectDialogAdapter.OnChooseListener() {
            @Override
            public void onSuccess(BankCardListModel.DataBean item) {
                if (selectListener != null) {
                    selectListener.onSure(item);
                }
                dismiss();
            }
        });
        list.setAdapter(adapter);
    }

}
